<?php

namespace App\Services\client;

use App\Helpers\Check;
use App\Models\client\Merchant;
use App\Services\BaseService;
use Illuminate\Support\Facades\Hash;

/**
 * 客户端管理员
 *
 * @Description:
 *
 * @Author: zhx
 *
 * @Date: 2023-09-05
 *
 * @LastEditTime: 2023-09-05
 */
class MerchantService extends BaseService
{
    /**
     * 校验登录
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public static function checkLogin($data)
    {
        try {
            $user = Merchant::where(['username' => $data['username']])->first();

            return $user;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 分页数据查询
     *
     * @Desc:
     *
     * @return mixed
     *
     * @author: zhx
     *
     * @Time: 2023/8/9 15:04
     */
    public static function getPageData($requestData)
    {
        [$perPage, $page, $orderBy, $orderDir] = [
            isset($requestData['perPage']) ? $requestData['perPage'] : 20,
            isset($requestData['page']) ? $requestData['page'] : 1,
            isset($requestData['orderBy']) ? $requestData['orderBy'] : 'id',
            isset($requestData['orderDir']) ? $requestData['orderDir'] : 'desc',
        ];
        $models = Merchant::query();
        if (isset($requestData['name'])) {
            $models->where('name', 'like', '%'.$requestData['name'].'%');
        }
        if (isset($requestData['username'])) {
            $models->where('username', 'like', '%'.$requestData['username'].'%');
        }
        if (isset($requestData['is_staff'])) {
            $models->where('is_staff', $requestData['is_staff']);
        }
        if (isset($requestData['state'])) {
            $models->where('state', $requestData['state']);
        }
        $user = $models->whereNull('deleted_at')->orderBy($orderBy, $orderDir)->paginate($perPage);

        return $user;
    }

    /**
     * 数据查询
     *
     * @Desc:
     *
     * @return mixed
     *
     * @author: zhx
     *
     * @Time: 2023/8/9 15:04
     */
    public static function getData($requestData)
    {
        [$orderBy, $orderDir] = [
            isset($requestData['orderBy']) ? $requestData['orderBy'] : 'id',
            isset($requestData['orderDir']) ? $requestData['orderDir'] : 'desc',
        ];
        $models = Merchant::query();
        if (isset($requestData['name'])) {
            $models->where('name', 'like', '%'.$requestData['name'].'%');
        }
        if (isset($requestData['username'])) {
            $models->where('username', 'like', '%'.$requestData['username'].'%');
        }
        if (isset($requestData['is_staff'])) {
            $models->where('is_staff', $requestData['is_staff']);
        }
        if (isset($requestData['state'])) {
            $models->where('state', $requestData['state']);
        }
        $user = $models->whereNull('deleted_at')->orderBy($orderBy, $orderDir)->get();

        return $user;
    }

    /**
     * 编辑
     *
     * @Desc:
     *
     * @return \Illuminate\Http\JsonResponse
     *
     * @throws \Illuminate\Validation\ValidationException
     *
     * @author: zhx
     *
     * @Time: 2023/8/10 14:44
     */
    // 在这里进行表单验证和用户更新逻辑
    public static function edit($id, $requestData)
    {
        $models = Merchant::query();
        $models->where('id', $id);

        return $models->update($requestData);
    }

    /**
     * 添加
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface create
     */
    public static function create($requestData)
    {
        $requestData['password'] = Hash::make($requestData['password']);
        $requestData['state'] = 1;
        $result = Merchant::create($requestData);

        return $result;
    }

    /**
     * 详细
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface info
     */
    public static function info($id)
    {
        $models = Merchant::query();

        return $models->where('id', $id)->first();
    }

    /**
     * 详细
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-03
     *
     * @Time: 16:14
     *
     * @Interface infoByUsername
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public static function infoByUsername($username)
    {
        $models = Merchant::query();

        return $models->where(['username' => $username])->first();
    }

    /**
     * 删除
     *
     * @Notes:
     *
     * @Author: zhx
     *
     * @Date: 2023-07-01
     *
     * @Time: 14:02
     *
     * @Interface info
     */
    public static function destroy($id)
    {
        $models = Merchant::query();

        return $models->where('id', $id)->update([
            'deleted_at' => date('Y-m-d H:i:s'),
        ]);
    }

    /**
     * 重置密码
     *
     * @return \Illuminate\Http\JsonResponse
     * User: zhx
     * Date: 2023/6/30 14:30
     */
    public static function resetPassword($id)
    {
        $models = Merchant::query();
        $password = Check::getRandStr(8);
        $ret = $models->where('id', $id)->update([
            'password' => password_hash($password, PASSWORD_BCRYPT),
            'reset_password' => $password,
        ]);

        return $ret;
    }

    /**
     * 状态更新
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public static function statusEdit($id)
    {
        $models = Merchant::query();
        $info = $models->where('id', $id)->first();
        if ($info->state == 1) {
            $ret = $models->where('id', $id)->update([
                'state' => 0,
            ]);
        } else {
            $ret = $models->where('id', $id)->update([
                'state' => 1,
            ]);
        }

        return $ret;
    }
}
